package com.codebuffer.nowcoder;

import java.util.ArrayList;
import java.util.List;

public class FindFirstCommonNode {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {

        List<ListNode> list = new ArrayList<>();
        while (null != pHead1 || null != pHead2){
            if(null != pHead1){
                if(list.contains(pHead1)){
                    return pHead1;
                }
                list.add(pHead1);
                pHead1 = pHead1.next;
            }
            if(null != pHead2){
                if(list.contains(pHead2)){
                    return pHead2;
                }
                list.add(pHead2);
                pHead2 = pHead2.next;
            }
        }
        return null;
    }
}
